package com.baiye.template.service.impl;

import com.baiye.template.dao.RoleDao;
import com.baiye.template.dao.UserDao;
import com.baiye.template.pojo.Role;
import com.baiye.template.pojo.User;
import com.baiye.template.service.PermissionService;
import com.baiye.template.service.RoleService;
import com.baiye.template.service.UserService;
import com.baiye.template.utils.PasswordEncoderUtils;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/26 16:21
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
//    @Autowired
//    private BCryptPasswordEncoder encoder;
    @Override
    public int regiseter(User u) {
        u.setPassword(PasswordEncoderUtils.getMd5(u.getPassword()));
        u.setCreatTime(new Date());
        return userDao.register(u);
    }

//    @Override
//    public User login(User u) {
//        u.setPassword(PasswordEncoderUtils.getMd5(u.getPassword()));
//        User user = userDao.login(u);
//        UserAuth auth = new UserAuth();
//        auth.setId(user.getId());
//        List<Role> roleList = roleService.getRoleByUserId(user.getId());
//        List<Permission> permissionList = new ArrayList<>();
//        for (Role role:roleList) {
//            permissionList.addAll(permissionService.getPermissionByRoleId(role.getId()));
//        }
//        auth.setPermissions(permissionList);
//        Gson gson = new Gson();
//        String str = gson.toJson(auth);
//        redisTemplate.opsForValue().set(String.valueOf(auth.getId()),str,5, TimeUnit.MINUTES);
//        return user;
//    }

    @Override
    public int updateUserInfo(User user) {
        return userDao.updateUserInfo(user);
    }

    @Override
    public List<User> getUser(User user, Integer pageIndex, Integer pageSize) {
        int start = (pageIndex-1)*pageSize;
        PageHelper.startPage(pageIndex,pageSize);
        return userDao.getUser(user, start,pageSize);
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        User user = userDao.loadUserByUsername(s);
        if(user==null){
            return null;
        }
        user.setRoles(roleDao.getRoleByUserIdExist(user.getId()));
        return  user;
    }
}
